var app = getApp()
Component({
  properties: {

  },
  data: {
  
  },
  methods: {

    getNumapi(sessionKey,encrypted,iv) {
  

      var params = {
        encrypted: encrypted,
        sessionKey: sessionKey,
        iv: iv
      }
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/bindPhone/auth',
        method: 'POST',
        data: params,
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.code == 200) {
              wx.showToast({
                title: '获取成功',
                icon: 'none'
              })


            var pages = getCurrentPages() //获取加载的页面
            var currentPage = pages[pages.length - 1] //获取当前页面
            currentPage.loginNum3()

              
          } else {
            wx.showToast({
              title: '授权失败,请再次授权',
              icon: 'none'
            })
          }
        }
      })
    },


    getSession(code, encrypted,iv) {
      var params = {
        code: code,
        isSdx: 1
      }
      wx.request({
        url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/checkAuth',
        method: 'POST',
        data: params,
        header: {
          "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
          "token": wx.getStorageSync('token'),
          "uid": wx.getStorageSync('uuid')
        },
        success: (res) => {
          if (res.data.success) {
            wx.removeStorageSync('sessionKey')
            wx.setStorageSync('sessionKey', res.data.data.session_key)
            this.getNumapi(res.data.data.session_key, encrypted,iv)
          }
        }
      })
    },
    getPhoneNumber (e) {

      var that = this
      // console.log(e)
      if (e.detail.errMsg !="getPhoneNumber:ok") {
        var pages = getCurrentPages() //获取加载的页面
        var currentPage = pages[pages.length - 1] //获取当前页面
        currentPage.loginNum()
      }else{

        console.log(e)



        wx.checkSession({
          success: function (res) {
            console.log('处于登录状态')

            that.getNumapi(wx.getStorageSync('sessionKey'),e.detail.encryptedData, e.detail.iv)
          },
          fail: function (res) {
            console.log("需要重新登录")
            wx.login({
              success: function (res) {
                if (res.code) {
                  console.log(res.code)


                  that.getSession(res.code, e.detail.encryptedData, e.detail.iv)
                }
              }
            })
          }
        })
        // wx.login({
        //   success: function (res) {
        //     if (res.code) {
        //       console.log(res.code)
        //       that.getNumapi(res.code, e.detail.encryptedData,e.detail.iv)

        //     }
        //   }
        // })

  
      }
    },

    changeModalCancel () {
      var pages = getCurrentPages() //获取加载的页面
      var currentPage = pages[pages.length - 1] //获取当前页面
      currentPage.loginNum()
    }
  }
})
